package org.learn.wsstomp.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * 〈DESCRIPTION〉
 *
 * @author xl
 * @version 1.0.0
 * @date 2019/6/17 20:53
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketStompConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/websocket").setAllowedOrigins("*").withSockJS();
        registry.addEndpoint("/ws").setAllowedOrigins("*");
    }

    /**
     * 不使用中间件
     *
     * @param registry
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/exchange", "/topic", "/queue");
        registry.setApplicationDestinationPrefixes("/app");
    }

    // 使用 rabbitmq 中间件
//    @Override
//    public void configureMessageBroker(MessageBrokerRegistry registry) {
//        registry.enableStompBrokerRelay("/exchange", "/topic", "/queue")
//                .setRelayHost("192.168.137.200")
//                .setRelayPort(61613)
//                .setSystemLogin("admin")
//                .setSystemPasscode("admin")
//                .setClientLogin("admin")
//                .setClientPasscode("admin");
//
//        registry.setApplicationDestinationPrefixes("/app");
//    }

}
